package com.goodfox.web.biz.forest.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.goodfox.web.biz.forest.entity.WoodlandFour;
import com.goodfox.web.biz.forest.entity.WoodlandPhoto;
import com.goodfox.web.biz.forest.entity.WoodlandSpecies;
import com.goodfox.web.biz.forest.mapper.WoodlandFourMapper;
import com.goodfox.web.biz.forest.mapper.WoodlandPhotoMapper;
import com.goodfox.web.biz.forest.mapper.WoodlandSpeciesMapper;
import com.goodfox.web.biz.forest.service.WoodlandService;
import com.goodfox.web.generic.provide.annotation.NoteExplain;
import com.goodfox.web.generic.provide.util.ParameterUtil;
import com.goodfox.web.generic.provide.util.TransformUtil;

@Transactional
@Service(value="woodlandService")
@NoteExplain(comment="林地管理")
public class WoodlandServiceImpl implements WoodlandService {

	/** log4j日志器 */
	private Logger logger=Logger.getLogger(WoodlandServiceImpl.class);
	
	@Autowired
	private WoodlandFourMapper woodlandFourMapper;

	@Autowired
	private WoodlandPhotoMapper woodlandPhotoMapper;
	
	@Autowired
	private WoodlandSpeciesMapper woodlandSpeciesMapper;
	
	@NoteExplain(comment="查询林地四至列表")
	public List<WoodlandFour> queryWoodlandFourList(String woodId){
		List<WoodlandFour> fourList=woodlandFourMapper.selectWoodlandFourListByWoodId(woodId);
		for(WoodlandFour four:fourList){
			if(four.getCorXY()==null || four.getCorXY().equals("")){
				if(four.getSort()!=null && four.getCorX()!=null && four.getCorY()!=null){
					four.setCorXY(four.getSort()+"："+four.getCorX()+"、"+four.getCorY());
				}
			}
		}
		return fourList;
	}

	@NoteExplain(comment="新增林地四至",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void addWoodlandFour(WoodlandFour entity,String loginLogId){
		if(entity.getFourName()!=null && entity.getFourName().length()>0){
			String sort=entity.getFourName().substring(0, 1);
			if(ParameterUtil.judgeInteger(sort)){
				entity.setSort(Long.valueOf(sort));
			}
		}
		if(entity.getSort()==null){
			Map map=new HashMap();
			map.put("woodId", entity.getWoodId());
			Long count=woodlandFourMapper.selectWoodlandFourListCount(map);
			entity.setSort(++count);
		}
		if(entity.getCorXY()!=null && !entity.getCorXY().equals("") && entity.getCorXY().indexOf("、")>=0){
			entity.setCorX(entity.getCorXY().substring(0,entity.getCorXY().indexOf("、")));
			entity.setCorY(entity.getCorXY().substring(entity.getCorXY().indexOf("、")+1));
		}
		woodlandFourMapper.insertWoodlandFour(entity);
	}
	
	@NoteExplain(comment="更新林地四至",paraDesc="oldEny 旧实体;newEny 新实体;loginLogId 登录日志Id")
	public void modifyWoodlandFour(WoodlandFour oldEny,WoodlandFour newEny,String loginLogId){
		if(newEny.getFourName()!=null && newEny.getFourName().length()>0){
			String sort=newEny.getFourName().substring(0, 1);
			if(ParameterUtil.judgeInteger(sort)){
				newEny.setSort(Long.valueOf(sort));
			}
		}
		if(newEny.getSort()==null){
			Map map=new HashMap();
			map.put("woodId", newEny.getWoodId());
			Long count=woodlandFourMapper.selectWoodlandFourListCount(map);
			newEny.setSort(++count);
		}
		if(newEny.getCorXY()!=null && !newEny.getCorXY().equals("") && newEny.getCorXY().indexOf("、")>=0){
			newEny.setCorX(newEny.getCorXY().substring(0,newEny.getCorXY().indexOf("、")));
			newEny.setCorY(newEny.getCorXY().substring(newEny.getCorXY().indexOf("、")+1));
		}
		woodlandFourMapper.updateWoodlandFour(newEny);
	}
	
	@NoteExplain(comment="移除林地四至",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void removeWoodlandFour(WoodlandFour entity,String loginLogId){
		woodlandFourMapper.deleteWoodlandFourById(entity.getFourId());
	}
	
	@NoteExplain(comment="查看林地四至")
	public WoodlandFour getWoodlandFour(String fourId){
		return woodlandFourMapper.selectWoodlandFourById(fourId);
	}
	
	@NoteExplain(comment="查询林地照片列表")
	public List<WoodlandPhoto> queryWoodlandPhotoList(String woodId){
		return woodlandPhotoMapper.selectWoodlandPhotoListByWoodId(woodId);
	}

	@NoteExplain(comment="新增林地照片",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void addWoodlandPhoto(WoodlandPhoto entity,String loginLogId){
		if(entity.getRealPath()!=null && !entity.getRealPath().equals("")){
			entity.setPhotoByte(TransformUtil.file2Bytes(new File(entity.getRealPath())));
		}
		woodlandPhotoMapper.insertWoodlandPhoto(entity);
	}
	
	@NoteExplain(comment="更新林地照片",paraDesc="oldEny 旧实体;newEny 新实体;loginLogId 登录日志Id")
	public void modifyWoodlandPhoto(WoodlandPhoto oldEny,WoodlandPhoto newEny,String loginLogId){
		if(newEny.getRealPath()!=null && !newEny.getRealPath().equals("")){
			newEny.setPhotoByte(TransformUtil.file2Bytes(new File(newEny.getRealPath())));
		}
		woodlandPhotoMapper.updateWoodlandPhoto(newEny);
	}
	
	@NoteExplain(comment="移除林地照片",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void removeWoodlandPhoto(WoodlandPhoto entity,String loginLogId){
		woodlandPhotoMapper.deleteWoodlandPhotoById(entity.getPhotoId());
	}
	
	@NoteExplain(comment="移除林地照片")
	public void autoRemoveWoodlandPhoto(String photoIdPath,HttpSession session){
		List<String> photoIdPathList=ParameterUtil.getListForString(photoIdPath, ";");
		String photoId=null;
		String photoPath=null;
		if(photoIdPathList.size()==2){
			photoId=photoIdPathList.get(0);
			photoPath=photoIdPathList.get(1);
		}
		WoodlandPhoto entity=woodlandPhotoMapper.selectWoodlandPhotoById(photoId);
		if(entity==null){
			String realPath=session.getServletContext().getRealPath("").replace("\\", "/")+photoPath;
			File file=new File(realPath);
			file.delete();
		}else{
			removeWoodlandPhoto(entity,session.getAttribute("loginLogId").toString());
			String realPath=session.getServletContext().getRealPath("").replace("\\", "/")+entity.getPhotoPath();
			File file=new File(realPath);
			file.delete();
		}
	}

	@NoteExplain(comment="查看林地照片")
	public WoodlandPhoto getWoodlandPhoto(String photoId){
		return woodlandPhotoMapper.selectWoodlandPhotoById(photoId);
	}

	@NoteExplain(comment="查询林地树种列表")
	public List<WoodlandSpecies> queryWoodlandSpeciesList(String woodId){
		return woodlandSpeciesMapper.selectWoodlandSpeciesListByWoodId(woodId);
	}

	@NoteExplain(comment="新增林地树种",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void addWoodlandSpecies(WoodlandSpecies entity,String loginLogId){
		woodlandSpeciesMapper.insertWoodlandSpecies(entity);
	}
	
	@NoteExplain(comment="更新林地树种",paraDesc="oldEny 旧实体;newEny 新实体;loginLogId 登录日志Id")
	public void modifyWoodlandSpecies(WoodlandSpecies oldEny,WoodlandSpecies newEny,String loginLogId){
		woodlandSpeciesMapper.updateWoodlandSpecies(newEny);
	}
	
	@NoteExplain(comment="移除林地树种",paraDesc="entity 新增实体;loginLogId 登录日志Id")
	public void removeWoodlandSpecies(WoodlandSpecies entity,String loginLogId){
		woodlandSpeciesMapper.deleteWoodlandSpeciesById(entity.getSpeciesId());
	}
}
